﻿using System;
using System.Collections.Generic;
using System.Text;
using Sys = Cosmos.System;
using Cosmos.Hardware;
using SplitOS.Utils;
using c = SplitOS.Utils.SplitOSConsole;

namespace SplitOS
{
    public class MainSystem : Sys.Kernel
    {
        protected override void BeforeRun()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("# SplitOS Version "+Core.Version);

            #region Memory Check
            Console.Write("# Memory check: " + Memory.GetMemory() + "MB");
            if (Memory.GetMemory() >= 256) c.WriteLine(" OK", ConsoleColor.Green);
            else c.WriteLine(" >256 MB RAM recommended!", ConsoleColor.Yellow);
            #endregion

            #region ACPI Initialization
            Console.Write("# Initializing ACPI");
            if (ACPI.Init()) c.WriteLine(" OK", ConsoleColor.Green);
            else c.WriteLine(" FAILED", ConsoleColor.Red);
            Console.Write("# Enabling ACPI");
            if (ACPI.Enable()) c.WriteLine(" OK", ConsoleColor.Green);
            else c.WriteLine(" FAILED", ConsoleColor.Red);
            #endregion

            #region FileSystem Initialization
            Console.Write("# Initializing Filesystem...");
            // Filesystem.Initialize();
            c.WriteLine(" Filesystem not implemented yet", ConsoleColor.Yellow);
            #endregion

            #region Initialize Keyboard
            uint scancode;
            Global.Keyboard.GetScancode(out scancode);
            Console.Write("# Region check: " + scancode.ToString());
            c.WriteLine(" OK", ConsoleColor.Green);
            #endregion

            c.WriteLine("Press any key to continue...", ConsoleColor.Gray);
            Console.Read();
            Console.Clear();

            #region Keyboard Layout/Language selection
            Core.PrintLogo();

            c.WriteLine("Press 'X' for German keyboard layout", ConsoleColor.Gray);
            c.WriteLine("Press another key if you want to use the default keyboard layout", ConsoleColor.Gray);
            List<Keyboard.KeyMapping> keymaps = new List<Keyboard.KeyMapping>();
            keymaps.Add(new Keyboard.KeyMapping(64, ConsoleKey.Q));
            if (Console.ReadKey().KeyChar == 'X')
            {
                Utils.Language.lang = Language.Languages.German;
            }
            else { Utils.Language.lang = Language.Languages.English; }
            #endregion

            Console.Clear();
        }

        protected override void Run()
        {
            while (true)
            {
                Core.PrintLogo();
                mainloop:
                Out.WritePrompt(); Validation.validate(Console.ReadLine());
                goto mainloop;
            }
        }

        protected override void AfterRun()
        {
            while (true)
            {
                Console.Write("Shutdown using ACPI "); c.Write("failed", ConsoleColor.Red); Console.WriteLine("!\n");
                Console.WriteLine("It's now safe to turn off the computer.");
            }
        }
    }
}